package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.PageParam;

import com.common.entity.Requirement;
import com.common.entity.Rsp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import com.wn.mapper.RequirementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class RequirementService implements BaseService<Requirement> {

    @Autowired
    RequirementMapper requirementMapper;



    @Override
    public PageInfo<Requirement> searchSomeByPage(PageParam<Requirement> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Requirement> requirementList=requirementMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Requirement> RequirementPageInfo = new PageInfo<Requirement>(requirementList);

            return RequirementPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Requirement> requirementList=requirementMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Requirement> RequirementPageInfo = new PageInfo<Requirement>(requirementList);

            return RequirementPageInfo;

        }


    }

    @Override
    public Requirement getOneById(String id) {
        Requirement oneById = requirementMapper.getOneById(id);
        if (!StringUtils.isEmpty(oneById)) {
            return oneById;
        }
        return null;
    }

    @Override
    public Rsp addOne(Requirement requirement) {
        String rId = UUID.randomUUID().toString().replaceAll("-", "")+"_req";
        String oId = UUID.randomUUID().toString().replaceAll("-","")+"_order";
        requirement.setId(rId);
        requirement.setCreatetime(new Date());

//        Order order = new Order();
//        order.setId(oId);
//        order.setOrderno(oId.substring(0,10));
//        order.setCreateuserid(requirement.getCreateuserid());
//        order.setStatus(rId);
//        order.setPayType(0);
//        order.setOrderstatus(0);
//        order.setIsprivate(0);
//
//        orderDao.insert(order);
        requirement.setOrderid(oId);

        return requirementMapper.addOne(requirement)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp update(Requirement requirement) {
        int i = requirementMapper.update(requirement);
        if(i>0){
            return Rsp.success(requirementMapper.getOneById(requirement.getId())) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(Requirement requirement) {
        requirement.setIsdeleted(1);
        int i = requirementMapper.update(requirement);
        if(i>0){
            return Rsp.success(requirementMapper.getOneById(requirement.getId())) ;
        }
        return Rsp.fail();
    }
}
